package com.skyjing.askit.java.temp;

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.annotation.IntDef;
import androidx.collection.SimpleArrayMap;


import com.skyjing.askit.AsTool;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 内置文件存储操作类
 */
public class AppFile {
    private static final SimpleArrayMap<String, AppFile> INTERNAL_FILE = new SimpleArrayMap<>();
    private String fileName;
    private int mode;


    /**
     * Context.MODE_PRIVATE：为默认操作模式，代表该文件是私有数据，只能被应用本身访问，
     * 在该模式下，写入的内容会覆盖原文件的内容，如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
     * Context.MODE_APPEND：模式会检查文件是否存在，存在就往文件追加内容，
     * 否则就创建新文件。
     * Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
     * MODE_WORLD_READABLE：表示当前文件可以被其他应用读取；
     * MODE_WORLD_WRITEABLE：表示当前文件可以被其他应用写入。
     *
     * @return
     */
//    public static AppFile getInstance() {
//        return getInstance("", Context.MODE_PRIVATE);
//    }

//    public static AppFile getInstance(int mode) {
//        return getInstance("", mode);
//    }
    @SuppressLint("WrongConstant")
    public static AppFile getInstance(String name) {
        return getInstance(name, Context.MODE_PRIVATE);
    }

    public static AppFile getInstance(String name,@AppFileAble.mode int mode) {
        if (isEmpty(name)) name = "AppFile";
        AppFile appFile = INTERNAL_FILE.get(name);
        if (appFile == null) {
            appFile = new AppFile(name, mode);
            INTERNAL_FILE.put(name, appFile);
        }
        return appFile;
    }

    private AppFile(String name) {
        fileName = name;
        this.mode = Context.MODE_PRIVATE;
    }

    private AppFile(String name, @AppFileAble.mode int mode) {
        fileName = name;
        this.mode = mode;
    }

    private static boolean isEmpty(String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public String read() {
        try {
            FileInputStream inStream = AsTool.getApp().openFileInput(this.fileName);
            byte[] buffer = new byte[1024];
            int hasRead = 0;
            StringBuilder sb = new StringBuilder();
            while ((hasRead = inStream.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, hasRead));
            }

            inStream.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void write(String msg) {
        // 步骤1：获取输入值
        if (msg == null) return;
        try {
            // 步骤2:创建一个FileOutputStream对象,MODE_APPEND追加模式
            FileOutputStream fos = AsTool.getApp().openFileOutput(this.fileName, this.mode);
            // 步骤3：将获取过来的值放入文件
            fos.write(msg.getBytes());
            // 步骤4：关闭数据流
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void write(int msg) {
        try {
            FileOutputStream fos = AsTool.getApp().openFileOutput(this.fileName, this.mode);
            fos.write(msg);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void write(byte[] msg) {
        if (msg == null) return;
        try {
            FileOutputStream fos = AsTool.getApp().openFileOutput(this.fileName, this.mode);
            fos.write(msg);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void write(byte[] msg, int off, int len) {
        if (msg == null) return;
        try {
            FileOutputStream fos = AsTool.getApp().openFileOutput(this.fileName, this.mode);
            fos.write(msg, off, len);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface AppFileAble {
        int PRIVATE = Context.MODE_PRIVATE;
        int APPEND = Context.MODE_APPEND;
        int READABLE = Context.MODE_WORLD_READABLE;
        int WRITEABLE = Context.MODE_WORLD_WRITEABLE;

        @Retention(RetentionPolicy.SOURCE)
        @IntDef({PRIVATE, APPEND, READABLE, WRITEABLE})
        @interface mode{}
    }
}
